import axios from "../http/http";
//请求所有todo数据
export function getAlltodo() {
	return axios.get("/todo");
}

//新增todo
// value  ===>  {done:false,title:"2132"}
export function reqAddtodo(value) {
	return axios.post("/todo", value);
}

//删除todo
export function reqDeleteTodo(id) {
	return axios.delete(`/todo/${id}`);
}

//修改某个todo
export function reqUpdateTodo(id, done) {
	return axios.patch(`/todo/${id}`, { done });
}

//清除已完成
export function reqClearALL(todo) {
	// 批量请求
	let promises = todo
		.filter((item) => item.done)
		.map((item) => {
			return axios.delete(`/todo/${item.id}`);
		});

	return Promise.all(promises);
}

//全选与取消全选
export function reqCheckedAll(todo, done) {
	//  批量处理
	let promises = todo.map((item) => {
		return axios.patch(`/todo/${item.id}`, { done });
	});

	//最终 prmoises  结果
	/* [
        axios.patch(`/todo/1`,{done:true}),
        axios.patch(`/todo/2`,{done:true}),
        axios.patch(`/todo/3`,{done:true})
    ]; */

	return Promise.all(promises);
}
